package com.chuanyang.driverh.mainmy;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 西风多少恨，吹不散眉弯！—唐大帅
 * @author 唐帅
 * 添加银行卡界面
 * created at  
 */

public class AddBanKCardActivity extends BaseTitleActivity {

    private TextView et_addcard_name;
    private TextView et_addcard_idcard;
    private TextView et_addcard_phone;
    private TextView et_addcard_cardcode;
    private TextView tv_addcard_add;
    private HashMap params;

    @Override
    protected int getContentResId() {
        return R.layout.my_act_addbankcard;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_addcard_name = (TextView) findViewById(R.id.et_addcard_name);
        et_addcard_idcard = (TextView) findViewById(R.id.et_addcard_idcard);
        et_addcard_phone = (TextView) findViewById(R.id.et_addcard_phone);
        et_addcard_cardcode = (TextView) findViewById(R.id.et_addcard_cardcode);
        tv_addcard_add = (TextView) findViewById(R.id.tv_addcard_add);
        tv_addcard_add.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("添加账户");
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case  R.id.tv_addcard_add:
                if(checkAll())
                {
                    addBankCard();
                }
                break;
        }
    }
    private boolean checkAll() {
        if(TextUtils.isEmpty(et_addcard_name.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"请输入您的姓名");
            return false;
        }
        if(TextUtils.isEmpty(et_addcard_idcard.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"请输入您的身份证号");
            return false;
        }
        if(!StringUtil.isCard(et_addcard_idcard.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"您的身份证号输入有误");
            return false;
        }
        if(TextUtils.isEmpty(et_addcard_phone.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"请输入您的手机号");
            return false;
        }
        if(!StringUtil.isPhoneNumber(et_addcard_phone.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"您的手机号输入有误");
            return false;
        }
        if(TextUtils.isEmpty(et_addcard_cardcode.getText().toString().trim()))
        {
            PrintUtil.toast(activityContext,"请输入您的银行卡号");
            return false;
        }
        return  true;
    }



    private void addBankCard() {
        DialogUtil.showLoadingDialog(activityContext,"添加银行卡...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("userId", SharedPrefUtil.getUserId());
        params.put("bankCard", et_addcard_cardcode.getText().toString().trim());
        params.put("idCard",et_addcard_idcard.getText().toString().trim());
        params.put("mobile", et_addcard_phone.getText().toString().trim());
        params.put("realName", et_addcard_name.getText().toString().trim());

        MyHttpUtil.addBankCard(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    onBackPressed();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }


}
